﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;

using IWshRuntimeLibrary;

namespace Space_Conquest___Installer
{
    public partial class Form1 : Form
    {

        private WshShellClass WshShell; 

        public Form1()
        {
            InitializeComponent();
        }

        #region Methods

        void Install()
        {

            // Show status
            InstallList.Items.Clear();
            InstallPanel.Visible = true;

            AddInstallMessage("Starting installer...");
            AddInstallMessage("Checking if Space Conquest is already installed...");

            // Do install logic
            if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Space Conquest"))
            {
                AddInstallMessage("Space Conquest is not installed. Installing now...");

                CopyDirectory(@"Space Conquest\", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Space Conquest");

                AddInstallMessage("Creating shortcuts in Start Menu...");
                CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Space Conquest\Space Conquest.exe");

                AddInstallMessage("Space Conquest has been successfully installed!");

            }
            else
            {
                AddInstallMessage("Space Conquest is already installed. Quitting install process");
            }
            

        }

        void AddInstallMessage(string message)
        {
            Thread.Sleep(50);
            InstallList.Items.Add(message);
            InstallList.SelectedIndex = InstallList.Items.Count - 1;
            InstallList.SelectedIndex = -1;
            this.Refresh();
        }

        void CopyDirectory(string sourceDirectory, string destinationDirectiory)
        {

            if (!sourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()))
                sourceDirectory += Path.DirectorySeparatorChar.ToString();

            if (!destinationDirectiory.EndsWith(Path.DirectorySeparatorChar.ToString()))
                destinationDirectiory += Path.DirectorySeparatorChar.ToString();

            if (!Directory.Exists(destinationDirectiory))
            {
                AddFileMessage("Creating folder: " + destinationDirectiory);
                Directory.CreateDirectory(destinationDirectiory);
            }

            foreach (string file in Directory.GetFiles(sourceDirectory))
            {
                AddFileMessage("Creating file: " + Path.GetFileName(file));
                System.IO.File.Copy(file, destinationDirectiory + Path.GetFileName(file));
            }

            foreach (string subDirectory in Directory.GetDirectories(sourceDirectory))
            {
                CopyDirectory(subDirectory, subDirectory.Replace(sourceDirectory, destinationDirectiory));
            }

        }

        void AddFileMessage(string message)
        {
            Thread.Sleep(10);
            InstallList.Items.Add(message);
            InstallList.SelectedIndex = InstallList.Items.Count - 1;
            InstallList.SelectedIndex = -1;
            this.Refresh();
        }

        void CreateShortcut(string exePath)
        {

            string startFolder = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\Space Conquest\";

            if (!Directory.Exists(startFolder))
            {
                Directory.CreateDirectory(startFolder);

                WshShell = new WshShellClass();

                IWshRuntimeLibrary.IWshShortcut MyShortcut;
                MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(startFolder + "Space Conquest.lnk");
                MyShortcut.TargetPath = exePath;
                MyShortcut.Description = "Space Conquest";
                MyShortcut.Save();

                WshShell = null;

            }

        }

        #endregion

        private void InstallDX_Click(object sender, EventArgs e)
        {
            Process.Start(@"DirectX 9\dxsetup.exe");
        }

        private void ViewSource_Click(object sender, EventArgs e)
        {
            Process.Start(@"Source Code\");
        }

        private void PlayGame_Click(object sender, EventArgs e)
        {
            Environment.CurrentDirectory += @"\Space Conquest\";
            Process.Start(@"Space Conquest.exe");
            Application.Exit();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Line1.Left = 0; Line1.Top = 0; Line1.Width = this.Width;
            Line2.Left = 0; Line2.Top = this.Height - 5; Line2.Width = this.Width;

            Line3.Left = 0; Line3.Top = 0 - 5; Line3.Height = this.Height;
            Line4.Left = this.Width - 5; Line4.Top = 0; Line4.Height = this.Height;

            this.Left = (Screen.PrimaryScreen.WorkingArea.Width / 2) - (this.Width / 2) - 5;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height / 2) - (this.Height / 2) - 10;

            MainPanel.Left = 61;
            MainPanel.Top = 111;

            InstallPanel.Left = 61;
            InstallPanel.Top = 111;

        }

        private void InstallGame_Click(object sender, EventArgs e)
        {

            Install();

        }

        private void InstallBack_Click(object sender, EventArgs e)
        {

            InstallPanel.Visible = false;

        }

    }
}
